package com.wds.common.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.wds.common.utils.SecurityUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Date;


@Configuration
public class MybatisPlusConfig implements MetaObjectHandler {

    /**
     * 3.4.0之后版本

     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(){
        MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
        mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        return mybatisPlusInterceptor;
    }

    @Override
    public void insertFill(MetaObject metaObject) {
        Long id = SecurityUtils.getCurrentLoginUser().getUser().getId();
        if (id == null) {
            id = -1L;
        }
        Date now  = new Date();
        metaObject.setValue("updateTime",now);
        metaObject.setValue("updateBy",id);
        metaObject.setValue("createTime",now);
        metaObject.setValue("createBy",id);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        Long id = SecurityUtils.getCurrentLoginUser().getUser().getId();
        if (id == null) {
            id = -1L;
        }
        Date now  = new Date();
        metaObject.setValue("updateTime",now);
        metaObject.setValue("updateBy",id);
    }
}
